package com.tuanzi.loan.web.bean.family.sms;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.tuanzi.loan.business.entity.sms.SmsCalculateTask;
import com.tuanzi.loan.business.entity.sms.SmsCalculateTaskStatus;
import com.tuanzi.loan.business.entity.sms.SmsSendTask;
import com.tuanzi.loan.business.service.sms.PhoneLocationService;
import com.tuanzi.loan.business.service.sms.SmsCalculateTaskService;
import com.tuanzi.loan.business.service.sms.SmsSendTaskService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.util.GsonUtils;
import com.tuanzi.loan.vo.request.family.sms.ExternalSmsCalculateCondition;
import com.tuanzi.loan.web.configuration.JSFBean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.Map;

import static com.tuanzi.loan.business.constants.DictionaryCode.*;

/**
 * @Author: chenweijun
 * @Date: 2018/11/1 15:57
 * @Description:
 */
@Data
@Slf4j
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class ExternalSmsConditionViewBean extends JSFBean {

    private SmsCalculateTask smsCalculateTask;
    private ExternalSmsCalculateCondition calculateCondition;
    private Map<String, String> provinces;
    private SmsSendTask smsSendTask;
    private Integer activeIndex = 0;

    @Autowired
    private SmsCalculateTaskService smsCalculateTaskService;

    @Autowired
    private SmsSendTaskService smsSendTaskService;

    @Autowired
    private PhoneLocationService phoneLocationService;


    @PostConstruct
    private void init() {

        String calculateTaskId = getParamFromRequest("calculateTaskId");
        if (StringUtils.isNotBlank(calculateTaskId)) {
            smsCalculateTask = smsCalculateTaskService.findOne(NumberUtils.toLong(calculateTaskId));
            calculateCondition = GsonUtils.toObject(smsCalculateTask.getCondition(), ExternalSmsCalculateCondition.class);
            log.info("calculateCondition=" + new Gson().toJson(calculateCondition));
            provinces = phoneLocationService.findDistinctProvince();
        }

        String sendTaskId = getParamFromRequest("sendTaskId");
        if (StringUtils.isNotBlank(sendTaskId)) {
            smsSendTask = smsSendTaskService.findOne(NumberUtils.toLong(sendTaskId));
            smsCalculateTask = smsCalculateTaskService.findOne(smsSendTask.getCalculateTaskId());
            calculateCondition = GsonUtils.toObject(smsCalculateTask.getCondition(), ExternalSmsCalculateCondition.class);
            provinces = phoneLocationService.findDistinctProvince();
        }
    }

    public boolean canEditCalculateTask() {
        return smsCalculateTask != null && smsCalculateTask.getStatus() != SmsCalculateTaskStatus.CALCULATING
                && smsCalculateTask.getStatus() != SmsCalculateTaskStatus.CALCULATE_DONE;
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(SMS_HIGH_QUALITY_USER_TYPE, SMS_ELIMINATE_BLACK_LIST, SMS_ELIMINATE_ACTION, SMS_ELIMINATE_ACTIVITY, SMS_USER_TYPE, PLATFORM, SMS_TEMPLATE_GROUP,
                SMS_TEMPLATE_BLACK_LIST, SMS_TEMPLATE_MARKETING, SMS_TEMPLATE_PULL_BACK, SMS_PLATFORM_CHANNEL, SMS_EXCLUDE_SENDED_DAY, SMS_EXTERNAL_CHANNEL);
    }

}
